package com.ysw.wisdomcampus.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.view.get
import com.ysw.wisdomcampus.R
import com.ysw.wisdomcampus.model.projectModel
import com.zhuangfei.timetable.core.SubjectUtils
import com.zhuangfei.timetable.core.TimetableView
import kotlinx.android.synthetic.main.fragment_subject.*
import kotlinx.coroutines.*
import org.koin.android.ext.android.get

class SubjectFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_subject, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        id_timetableView.apply {
            scrollView.background = ContextCompat.getDrawable(requireContext(), R.drawable.bg)
            setDataSource(projectModel.subjects)
            curTerm = "大三:第一学期"
            curWeek = 1
            setOnSubjectItemClickListener { v, subjectList ->
                val subject = subjectList[(Math.random() * subjectList.size).toInt()]
                AlertDialog.Builder(requireContext())
                    .setTitle(subject.name)
                    .setMessage("教室: ${subject.room} \n老师: ${subject.teacher}")
                    .setPositiveButton("知道啦") { dialog, _ -> dialog.dismiss() }
                    .create()
                    .show()
            }
            setOnSubjectItemLongClickListener { view, day, start ->
                val textView = ((((view as ViewGroup)[0] as ViewGroup)[0])) as AppCompatTextView
                GlobalScope.launch(Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        projectModel.setDone(
                            day, start
                        )
                    }
                    projectModel
                        .toast("${textView.text} 签到成功！")
                }
            }
            showTimetableView()
        }
    }
}